﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace TRAC_Demo
{
    // T.R.A.C.
    // Main Author: Ryan Redburn
    // Date: 10/20/2013

    public partial class FormMail : Form
    {
        #region [ Fields ]

        private List<string> userMail;

        #endregion

        #region [ Contructors ]

        public FormMail()
        {
            InitializeComponent();
            this.BackColor = CurrentUser.UserColor;
        }

        #endregion

        #region [ Events ]

        #region [ Compose ]

        // Sends the currently written message as long as all required fields are filled
        private void btnSend_Click(object sender, EventArgs e)
        {
            try
            {
                if (txtTitleCompose.Text != "" && txtBodyCompose.Text != "" && cmbRecepient.SelectedItem != null)
                {
                    MailCarrier.SendMessage(txtTitleCompose.Text, txtBodyCompose.Text, (string)cmbRecepient.SelectedItem);
                    ClearComposeFields();
                    MessageBox.Show("Your message was sent successfully.", "Message Sent");
                }
                else
                {
                    MessageBox.Show("One or more required fields has been left empty.", "Empty Required Fields");
                }
            }
            catch (InvalidOperationException)
            {
                MessageBox.Show("The user you are trying to send to does not have an account.", "Nonexistant User");
            }
            catch (SqlException)
            {
                MessageBox.Show("The application has encoutered an error with the database, please contact the application developer for assisstance.", "Database Error");
            }
        }

        // Clears the compose fields
        private void btnClear_Click(object sender, EventArgs e)
        {
            ClearComposeFields();
        }

        #endregion

        #region [ Inbox ]

        // Views the message that currently has focus
        private void btnView_Click(object sender, EventArgs e)
        {
            try
            {
                List<string> message = MailCarrier.GetMessage(GetMailID());
                txtTitleInbox.Text = message[1];
                txtBodyInbox.Text = message[2];
                txtFromInbox.Text = message[0];
            }
            catch (SqlException)
            {
                MessageBox.Show("The application has encoutered an error with the database, please contact the application developer for assisstance.", "Database Error");
            }
        }

        // Allows the user to reply to a message they are currently viewing
        // Fills in the Title and recepient fields automatically
        private void btnReply_Click(object sender, EventArgs e)
        {
            if (txtTitleInbox.Text != "" && txtBodyInbox.Text != "" && txtFromInbox.Text != "")
            {
                ClearComposeFields();
                txtTitleCompose.Text = "RE: " + txtTitleInbox.Text;
                cmbRecepient.SelectedItem = txtFromInbox.Text;
                ClearInboxFields();
                tabMain.SelectedTab = tabCompose;
                txtBodyCompose.Focus();
            }
            else
            {
                MessageBox.Show("You have not selected a message to reply to.", "No Message Selected");
            }
        }

        // Refreshes the inbox list
        private void btnRefresh_Click(object sender, EventArgs e)
        {
            try
            {
                RefreshInbox(CurrentUser.EmpID);
            }
            catch (SqlException)
            {
                MessageBox.Show("The application has encoutered an error with the database, please contact the application developer for assisstance.", "Database Error");
            }
        }

        // Deletes the currently selected message
        private void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                if (MessageBox.Show("Are you sure you want to delete this message?", "Confirm Delete",
                    MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    MailCarrier.DeleteMessage(int.Parse(userMail[lstInbox.SelectedIndex].Substring(0, 1)));
                    RefreshInbox(CurrentUser.EmpID);
                    ClearInboxFields();
                }
            }
            catch (SqlException)
            {
                MessageBox.Show("The application has encoutered an error with the database, please contact the application developer for assisstance.", "Database Error");
            }
        }

        #endregion

        // Fill the recepient combo box and inbox mail list witht eh appropriate entries
        private void FormMail_Load(object sender, EventArgs e)
        {
            try
            {
                cmbRecepient.Items.AddRange(MailCarrier.RecepientList(CurrentUser.EmpID).ToArray());
                RefreshInbox(CurrentUser.EmpID);
            }
            catch (SqlException)
            {
                MessageBox.Show("The application has encoutered an error with the database, please contact the application developer for assisstance.", "Database Error");
            }
        }

        // Closes the form pending user confirmation
        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        #endregion

        #region [ Helper Methods ]

        // Clears all compose fields
        private void ClearComposeFields()
        {
            txtTitleCompose.Clear();
            txtBodyCompose.Clear();
            cmbRecepient.SelectedItem = null;
        }

        // Clears all inbox fields
        private void ClearInboxFields()
        {
            txtTitleInbox.Clear();
            txtBodyInbox.Clear();
            txtFromInbox.Clear();
        }

        // Populates the list box with the message headers for all of the user's mail
        private void RefreshInbox(int employeeID)
        {
            userMail = MailCarrier.GetMail(CurrentUser.EmpID);
            lstInbox.Items.Clear();
            for (int i = 0; i < userMail.Count; i++)
            {
                lstInbox.Items.Add(userMail[i]);
            }
        }

        // Gets the mail id of the selected message
        private int GetMailID()
        {
            int mailID = int.Parse(userMail[(lstInbox.SelectedIndex)].Substring(0, 1));
            return mailID;
        }

        #endregion
    }
}
